﻿//包参与对象
packageContent.partObj = function(partObjNumber,objId) {
	var _partObjNumber = partObjNumber;
	var _objId = objId;
	//定义参与对象
	var _objMap = partObjMap;
	var appendOption;
	var _initPartObjComp = function() {
		$("#selectProdSpec").window( {
			title : '选择产品规格',
			width : 700,
			height : 390,
			modal : true,
			shadow : false,
			closed : true,
			resizable : false,
			draggable : true,
			minimizable : false,
			maximizable : false
		});

		//初始化对象列表
		var keys = _objMap.keys()
		for ( var i = 0; i < keys.length; i++) {
			appendOption = $("<option>");
			appendOption.val(keys[i]);
			appendOption.html(_objMap.get(keys[i]));
			$("#p_partObj").append(appendOption);
		}
		
		//新增默认初始值
		//序列的初始化，新增默认选择
		if(Validate.isNull(_objId)) {
			$("#p_partObj_seq").val(packageContent.getMaxIndex(MAXTYPE.partObj));
			$("#obj_ch_y").attr("checked", true);
		}
		
		_selectCompListener();
	}

	var _disableComp = function() {
		//参与对象设定再修改时，不允许修改
		$("#p_partObj").attr("disabled","true");
		$("#p_partObj_search").hide();

		if ((_objId == OBJECT.PARTY)) {
			$("#obj_ch_y").attr("disabled", "true");
			$("#obj_ch_n").attr("disabled", "true");
			$("#obj_ch_d").attr("disabled", "true");
			$("#defaultValue").attr("disabled", "true");

		}

	}

	/**
	 * 监听参与对象的选择
	 */
	var _selectCompListener = function () {
		$("#p_partObj").bind("change", function() {
			if (($("#p_partObj").val()) == OBJECT.PARTY) {
				$("#obj_ch_y").attr("checked", false);
				$("#obj_ch_n").attr("checked", false);
				$("#obj_ch_d").attr("checked", false);
				$("#obj_ch_y").attr("disabled", "true");
				$("#obj_ch_n").attr("disabled", "true");
				$("#obj_ch_d").attr("disabled", "true");
				$("#defaultValue").attr("disabled", "true");
			} else {
				$("#obj_ch_y").removeAttr("disabled");
				$("#obj_ch_n").removeAttr("disabled");
				$("#obj_ch_d").removeAttr("disabled");
				$("#defaultValue").removeAttr("disabled");
			}

		});
	}

	var _initPartObj = function() {
		//基本信息赋值
		if ((pack.getPackObjMap() == undefined)
				|| (pack.getPackObjMap().get(_partObjNumber) == null)) {
			return;
		}
		//屏蔽
		_disableComp();


		var _packageObj = pack.getPackObjMap().get(_partObjNumber);
		var _queryObjName;
		var isDiffer = true;
		//添加内存中增加的option
		if (!_objMap.containsKey(_packageObj.getObjId())) {
			appendOption = $("<option>");
			appendOption.val(_packageObj.getObjId());
			var _queryObjName = _getProdSpecName(_packageObj.getObjId());
			//存在别名
			if (_queryObjName !=_packageObj.getObjectName()) {
				appendOption.html(_queryObjName);
				isDiffer = false;
			}
			//添加到内存中避免重复查询
			_objMap.put(_packageObj.getObjId(),_queryObjName);

			$("#p_partObj").append(appendOption);
		} else {
			var _queryObjName = _objMap.get(_packageObj.getObjId());
			//存在别名
			if (_queryObjName !=_packageObj.getObjectName()) {
				isDiffer = false;
			}
		}

		//参与对象
		$("#p_partObj").val(_packageObj.getObjId());

		//新装，已有，不需要
		var isNew = _packageObj.getIsNew();
		if (isNew && isNew.indexOf(OBJECT.OBJACTION.Y) != -1) {
			$("#obj_ch_y").attr("checked", true);
		}
		if (isNew && isNew.indexOf(OBJECT.OBJACTION.N) != -1) {
			$("#obj_ch_n").attr("checked", true);
		}
		if (isNew && isNew.indexOf(OBJECT.OBJACTION.D) != -1) {
			$("#obj_ch_d").attr("checked", true);
		}

		//别名
		if (!isDiffer) {
			$("#p_partObj_alias").val(_packageObj.getObjectName());
			_packageObj.setAlias($("#p_partObj_alias").val());
		}
		if (_packageObj.getAlias()) {
			$("#p_partObj_alias").val(_packageObj.getAlias());
		}
		var objDefaultValue = _packageObj.getDefaultValue();
		$("#defaultValue").val(objDefaultValue);


		//对象序号
		$("#p_partObj_seq").val(_packageObj.getSeq());

	};

	//打开查询查询面板
	var _openQueryWindow = function() {
		$("#selectProdSpec").window('open');
		$('#selectProdSpec').window('refresh',
				'PackagePathDispatcher?path=selectProdSpec.jsp');
	}

	var _validate = function() {
		if (Validate.isNull($("#p_partObj").val())) {
			PACK.ALERT.warn("请选择【参与对象】！");
			return false;
		}
		if (Validate.isNull($("#p_partObj_seq").val())) {
			PACK.ALERT.warn("请设置【参与对象】顺序！");
			return false;
		}


		if ((parseInt($("#p_partObj_seq").val()) != $("#p_partObj_seq").val()) || $("#p_partObj_seq").val() < 1) {
			PACK.ALERT.warn("【参与对象】序号需为大于1的数字");
			return false;
		}

		//验证非【客户】的情况
		if ($("#p_partObj").val() != OBJECT.PARTY) {
			//加入方式,是否新装，必须选其一
			var _isNew ="";
			$("input[id^='obj_ch_']").each(function() {
				if ($(this).attr("checked")) {
					_isNew = _isNew + $(this).val();
				}
			})
			if ((_isNew.indexOf(OBJECT.OBJACTION.Y) == -1) &&
					(_isNew.indexOf(OBJECT.OBJACTION.N) == -1)) {
				PACK.ALERT.warn("加入方式：【是】【否】必须选择一个");
				return false;
			}
				//校验加入方式默认值必须是已经选中的接入方式
			var dv= $("#defaultValue").val();
			if(dv){
				if ((_isNew.indexOf(dv) == -1) ) {
					PACK.ALERT.warn("默认的加入方式：必须是已经勾选的加入方式");
					return false;
				}

			}

		}


		//校验别名长度
		if ($("#p_partObj_alias").val() && $("#p_partObj_alias").val().length > 10) {
			PACK.ALERT.warn("【别名】长度不能大于10个字符");
			return false;
		}

		return true;
	}

	//保存
	var _savePartObj = function(_partObjNumber) {
		//校验
		var _flag = _validate();
		if (!_flag) {
			return _flag;
		}
		var objName = $("#p_partObj").find("option:selected").text();
		var defaultValue =  $("#defaultValue").val();
		var objId = $("#p_partObj").val();
		var objAlias = $("#p_partObj_alias").val();
		var objType = OBJECT.OBJTYPE.PROD_SPEC;
		var objSeq = parseInt($("#p_partObj_seq").val());
		//客户对象
		if (objId == '-1') {
			objType = OBJECT.OBJTYPE.PARTY;
		}

		//加入方式,是否新装，已有，不需要
		var isNew = "";
		$("input[id^='obj_ch_']").each(function() {
			if ($(this).attr("checked")) {
				isNew = isNew + $(this).val();
			}
		})

		//重新赋值:对象
		var _packageObj = new PackageObject();
		_packageObj.setObjId(objId);
		_packageObj.setObjType(objType);
		if (objAlias) {
			_packageObj.setObjectName(objAlias);
		} else {
			_packageObj.setObjectName(objName);
		}
		_packageObj.setAlias(objAlias);
		_packageObj.setIsNew(isNew);
		_packageObj.setObjectNumber(_partObjNumber);
		//xiesh add 0619
		_packageObj.setDefaultValue(defaultValue);
		_packageObj.setSeq(objSeq);

		var packObjMap;
		if (pack.getPackObjMap() == undefined) {
			packObjMap = new HashMap();
		} else {
			packObjMap = pack.getPackObjMap();
		}
		packObjMap.put(_partObjNumber, _packageObj);
		pack.setPackObjMap(packObjMap);

		//展示名
		var showName = objAlias ? ('['+objSeq+'] '+objAlias) : ('['+objSeq+'] '+objName);
		$("#" + _partObjNumber).html(showName);
		$("#" + _partObjNumber).attr("objId",objId);

		//参与对象seq
		$("#" + _partObjNumber).attr("partObjSeq",objSeq);
		
		return _flag;
	}

	/**
	 * 获取产品规格的名称
	 * @param {Object} prodSpecId
	 * @return {TypeName}
	 */
	var _getProdSpecName = function(prodSpecId) {
		var _prodSpecName;
		$.ajax({
					type:"POST",
					url:"../util/queryProdSpecList.action",
					dataType:"json",
					async:false,
					data:{prodSpecId:prodSpecId},
					success:function(data){
						_prodSpecName = data[0].NAME;
					}
				});

		return _prodSpecName;
	}
	//关闭
	var _doClose = function() {
		$("#openPartObjPage").window('close');
	}

	//初始化包参与对象
	var _init = function() {
		_initPartObjComp();
		_initPartObj();

		$("#p_partObj_save").bind("click", function() {
			var _flag = _savePartObj(_partObjNumber);
			if (_flag) {
				_doClose();
			}

		});
		$("#p_partObj_cancel").bind("click", function() {
			_doClose();
		});
		$("#p_partObj_search").bind("click", function() {
			_openQueryWindow();
		});
	}

	_init();
}
